Using Regular Expressions in Macromedia Central
Table of Contents
Testing Tips
Because JavaScript follows the same ECMAScript standard as Macromedia Central, I find that an easy way to write and test regular expressions for Central is in an HTML file. The main difference I've found between these two implementations is that JavaScript uses exec(), whereas Central uses match(). The good news is that Central also allows exec(), which does the same thing as match(). However, there are other differences: the JavaScript implementation has more String methods that use regular expressions, and the Central implementation has a few more properties to set. Because you are not guaranteed that what works in JavaScript works in Central, it's a good idea to test all scripts in Central as a final step if you wrote them in JavaScript.
Here is an easy example of the contents of an HTML file I created to test one of the previous samples:
<script>
var re = new RegExp("\\bh\\w+\\b");
// note using 'exec()' in place of 'match()'
document.writeln(re.exec("hello world") + "<br>"); // displays "hello"
document.writeln(re.exec("my name is haldir") + "<br>"); // displays "haldir"
document.writeln(re.exec("this won't match?") + "<br>"); // displays null
document.writeln(re.test("hello world") + "<br>"); // displays "hello"
document.writeln(re.test("my name is haldir") + "<br>"); // displays "haldir"
document.writeln(re.test("this won't match?") + "<br>"); // displays null
</script>
To test this in Central, I simply create a text box and send output to it.
Alternately, you could use trace(); just remember to set debug.enabled = true. Regular expressions only work in Central, not Flash, so your apps need to be running in the Central framework in order for these new APIs to work.
Useful Examples
One area in which regular expressions are extremely useful is form validation. Below is a list a regular expressions that you can use for validating. Although the names should be descriptive enough to determine their purpose, after reading and understanding this article you should have the necessary skills to read the regular expression syntax in Listing 1 and see exactly what each one does.
Listing 1. Useful Regular Expressions
// matches alphanumeric strings with at least one character
var reWordPattern = new RegExp("^\\w+$");
// matches all letter strings with at least one character
var reLettersPattern = new RegExp("^[A-Za-z]+$");
// matches all digit strings with at least one character
var reDigitPattern = new RegExp("^\\d+$");
// matches exactly two uppercase letters
var reStatePattern = new RegExp("^[A-Z]{2}$");
// matches exactly five digits or five digits plus '-' plus four digits
var reZipPattern = new RegExp("^(?:\\d{5}|\\d{5}-\\d{4})$");
// matches either xxx-xxx-xxxx or (xxx)xxx-xxxx
var rePhonePattern = new RegExp("^(?:\\d{3}-\\d{3}-\\d{4}|\\(\\d{3}\\)\\s?\\d{3}-\\d{4})$");
// matches format mm/dd/yyyy with one-digit month or day possible and two-digit year possible
var reDatePattern = new RegExp("^(\\d{1,2})/(\\d{1,2})/(\\d{2}|\\d{4})$");
// matches at least one character (and then possible '.' which must be followed by at least one more
// character), then a '@', and then at least two alphanumeric words separated by a '.' but possibly more
var reEmailPattern = new RegExp("^(\\w(?:\\w|\\.\\w)*)@(\\w+\\.\\w(?:\\w|\\.\\w)*)$");
// matches the a valid url string; note that we're capturing the domain in memory (discussed later)
var reUrlPattern = new RegExp("^(http|ftp|https|file)://(.*?)/.*$");
Note: Two metacharacters I use in Listing 1 but did not really discuss earlier are "|", which is read as "or", and "(?:", which works exactly the same as "(" except that no memory is stored when a group is made this way. If any of the following lines are unintelligible, look over the part of this article that corresponds to the symbols you see or review the Perl documentation on regular expression syntax.
You could use these regular expressions in the following ways for validation using the RegExp test() method:
function validate (form_mc) {
var userName = form_mc.userName_txt.text; // user name that was input
var zipCode = form_mc.zipCode_txt.text; // zip code that was input
var phone = form_mc.phone_txt.text; // phone number
return (reWordPattern.test(userName) && reZipPattern.test(zipCode) && rePhonePattern.test(phone));
}
I designed the reDatePattern, reEmailPattern, and reUrlPattern examples in Listing 1 to have some useful memory in them. The other examples in Listing 1 do not have very interesting parts. Here's what I mean. A date has interesting parts—month, day, year—that could be used separately from the whole date. Likewise, an e-mail address has a username and domain name. But a word, number, or phone number doesn't really contain any parts of interest. A possible exception would be the area code from a phone number. Converting rePhonePattern to remember the area code is left as an exercise for you to tackle.
When reDatePattern.match() succeeds it returns an array with four elements. The first element is (as always) the entire portion of the string matched. The second, third, and fourth elements are defined using the "( )" memory metacharacters to be the month, day, and year, respectively. Meanwhile, reEmailPattern.match() returns three elements: The first is the whole match, the second is the user_name (portion before the "@"), and the third is the domain name (portion after the "@"). Finally, reUrlPattern.match() returns three elements: The first is the entire string matched, the second is the protocol used, and the third is the domain. Here are some examples of their usage:
if (reDatePattern.test(aDate)) {
// passed so let's grab the results
var results = reDatePattern.match(aDate);
// note: since this pattern matches the whole string aDate == results[0]
var month = results[1];
var day = results[2];
var year = results[3];
}
if (reEmailPattern.test(anEmailAddr)) {
// passed so let's grab the results
var results = reEmailPattern.match(anEmailAddr);
var name = results[1];
var domain = results[2];
}
if (reUrlPattern.test(myUrl)) {
// passed so let's grab the results
var results = reUrlPattern.match(myUrl);
var protocol = results[1];
var domain = results[2];
}